﻿Imports System.ComponentModel
Partial Public Class ExpensesWindow
    'Public WithEvents Data As PetrolTransactionsList = PetrolTransactionsList.GetList()
    Private WithEvents petrolTrans As PetrolTransactionsClass
    Public WithEvents oWindowList As OpenedWindowList = OpenedWindowList.GetList()
    Private WithEvents oWindow As OpenedWindowClass
    Public WithEvents expenseList As ExpensesList = ExpensesList.GetList()
    Private WithEvents expenseTrans As ExpensesClass
    Public WithEvents expenseTypeList As ExpenseTypeList = expenseTypeList.GetList()
    Private WithEvents expenseTypeClass As ExpenseTypeClass

    Dim expensesDB As New IETrackerDataClassesDataContext
    Dim frmIsDirty As Boolean = False
    Private _pageIsValid As Boolean = True
    Dim editFlag As Integer
    Dim al As New ArrayList
    Dim comboClassTextbox As New TextBox


    '#Region "Window Events"

    '    Private Sub ExpensesWindow_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ExpensesWindow.Closing
    '        If frmIsDirty Then
    '            If MessageBox.Show("There are unsaved entries. Do you really want to exit?", "Exit", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.No Then
    '                e.Cancel = True
    '            End If
    '        End If
    '    End Sub

    '    Private Sub ExpensesWindow_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
    '        Dim ied As IEnumerable(Of OpenedWindowClass) = From d In oWindowList Where d.WindowID = Me.Name
    '        oWindowList.Remove(ied.First)

    '        If Application.Current.Windows.Count = 1 Then
    '            Application.Current.Windows(0).Activate()
    '        End If
    '    End Sub

    '    Private Sub ExpensesWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    '        comboClassTextbox = TryCast(ComboBoxExpenseType.Template.FindName("PART_EditableTextBox", ComboBoxExpenseType), TextBox)
    '        AddHandler comboClassTextbox.TextChanged, AddressOf ComboBox_TextChanged

    '        LoadExpenseTypes()
    '        noSelectedData()
    '        HookupChangeEventHandlers()
    '        LoadSubHeader()
    '    End Sub
    '#End Region

    '#Region "RibbonButton Click events"

    '    Private Sub RibbonButtonExpenseType_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonExpenseType.Click
    '        Dim ExpenseTypeWindow As New ExpenseTypes
    '        ExpenseTypeWindow.Name = "window" & oWindowList.Count
    '        ExpenseTypeWindow.Show()
    '        oWindow = New OpenedWindowClass(ExpenseTypeWindow.Name, "Setup Expense Type", ExpenseTypeWindow.Name, "Images/Gas-Station-icon.png")
    '        oWindowList.Add(oWindow)
    '    End Sub

    '    Private Sub RibbonButtonCancel_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonCancel.Click
    '        If editFlag = HelperClass.EditMode.Add Then
    '            HelperClass.ClearTextBox(GridExpense)
    '            HelperClass.ClearComboBox(GridExpense)
    '            DatePickerTransactionDate.SelectedDate = Nothing
    '            noSelectedData()
    '            frmIsDirty = False
    '        ElseIf editFlag = HelperClass.EditMode.Edit Then

    '            If listBoxExpense.SelectedIndex >= 0 Then
    '                With listBoxExpense.SelectedItem
    '                    ComboBoxExpenseType.SelectedValue = .expensetypeid
    '                    DatePickerTransactionDate.SelectedDate = .expensedate
    '                    TextBoxAmount.Text = .expenseAmount
    '                    CheckBoxOneOff.IsChecked = .expenseoneoff
    '                    TextBoxComment.Text = .expensecomment
    '                End With
    '                frmIsDirty = True
    '            Else
    '                frmIsDirty = False
    '            End If

    '            setButtonProperties()
    '        End If
    '    End Sub


    '    Private Sub RibbonButtonDelete_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonDelete.Click
    '        Dim index As Integer = listBoxExpense.SelectedIndex
    '        If index >= 0 Then
    '            Try
    '                Dim warningString As String = "Do you really want to delete this transaction?" & ControlChars.Lf & ControlChars.Lf & listBoxExpense.SelectedItem.expensetype & " - " & listBoxExpense.SelectedItem.expensedate
    '                If System.Windows.MessageBox.Show(warningString, "Delete Transaction", MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.Yes Then
    '                    Dim selectedExpenseTransactionID As Integer = listBoxExpense.SelectedValue.expenseid
    '                    'if Petrol
    '                    If listBoxExpense.SelectedValue.expensetypeId = 1 Then
    '                        Dim deleteFromPetrol = From petrol In expensesDB.Petrols _
    '                                                     Where petrol.TransactionID = selectedExpenseTransactionID
    '                        expensesDB.Petrols.DeleteOnSubmit(deleteFromPetrol.First)
    '                        expensesDB.SubmitChanges()
    '                        Dim iPetrolTransactionClass As IEnumerable(Of PetrolTransactionsClass) = From d In Data Where d.TransactionID = selectedExpenseTransactionID
    '                        Data.Remove(iPetrolTransactionClass.First)
    '                    End If

    '                    Dim deleteFromTransaction = From trans In expensesDB.Transactions _
    '                                                Where trans.TransactionID = selectedExpenseTransactionID
    '                    expensesDB.Transactions.DeleteOnSubmit(deleteFromTransaction.First)
    '                    expensesDB.SubmitChanges()

    '                    Dim iExpensesClass As IEnumerable(Of ExpensesClass) = From d In expenseList Where d.ExpenseId = selectedExpenseTransactionID
    '                    expenseList.Remove(iExpensesClass.First)

    '                    HelperClass.ClearTextBox(GridExpense)
    '                    HelperClass.ClearComboBox(GridExpense)
    '                    DatePickerTransactionDate.SelectedDate = Nothing
    '                    noSelectedData()
    '                End If

    '            Catch ex As Exception
    '                MessageBox.Show("Error encountered while deleting the record." & ControlChars.Lf & ControlChars.Lf & ex.Message.ToString, "Delete Error", MessageBoxButton.OK, MessageBoxImage.Error)
    '            End Try
    '        End If
    '    End Sub

    '    Private Sub RibbonButtonNew_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonNew.Click
    '        mainOverlay.Visibility = Visibility.Collapsed
    '        editFlag = HelperClass.EditMode.Add
    '        ComboBoxExpenseType.IsEnabled = True

    '        listBoxExpense.SelectedIndex = -1
    '        HelperClass.ClearTextBox(GridExpense)
    '        HelperClass.ClearComboBox(GridExpense)
    '        DatePickerTransactionDate.SelectedDate = Nothing

    '        frmIsDirty = True
    '        setButtonProperties()
    '    End Sub

    '    Private Sub RibbonButtonPetrol_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonPetrol.Click
    '        Dim petrolWindow As New NewPetrol
    '        petrolWindow.Name = "window" & oWindowList.Count
    '        petrolWindow.Show()
    '        oWindow = New OpenedWindowClass(petrolWindow.Name, "Petrol", petrolWindow.Name, "Images/Gas-Station-icon.png")
    '        oWindowList.Add(oWindow)
    '    End Sub

    '    Private Sub RibbonButtonSave_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RibbonButtonSave.Click
    '        pageIsValid(DatePickerTransactionDate.Name, EntryValidation.ValidateDatePicker(DatePickerTransactionDate))
    '        pageIsValid(ComboBoxExpenseType.Name, EntryValidation.ValidateEditableCombo(ComboBoxExpenseType, comboClassTextbox))
    '        pageIsValid(TextBoxAmount.Name, EntryValidation.ValidateText(TextBoxAmount))

    '        If _pageIsValid Then
    '            Try
    '                Dim transactionTypeID As Integer = ComboBoxExpenseType.SelectedValue
    '                If editFlag = HelperClass.EditMode.Add Then
    '                    Dim saveNewTransaction As New Transaction
    '                    If transactionTypeID = 0 Then
    '                        Dim saveNewTransactionDescription As New TransactionDescription
    '                        With saveNewTransactionDescription
    '                            .TransactionDescription = ComboBoxExpenseType.Text
    '                            .IsAutoDeduct = False
    '                        End With
    '                        expensesDB.TransactionDescriptions.InsertOnSubmit(saveNewTransactionDescription)
    '                        expensesDB.SubmitChanges()

    '                        Dim saveNewTransactionType As New TransactionType
    '                        With saveNewTransactionType
    '                            .TransactionTypeDescID = 2
    '                            .TransactionDescID = saveNewTransactionDescription.TransactionDescriptionID
    '                            .OneOff = CheckBoxOneOff.IsChecked
    '                        End With
    '                        expensesDB.TransactionTypes.InsertOnSubmit(saveNewTransactionType)
    '                        expensesDB.SubmitChanges()

    '                        transactionTypeID = saveNewTransactionType.TransactionTypeID
    '                    End If

    '                    With saveNewTransaction
    '                        .TransactionAmount = TextBoxAmount.Text
    '                        .TransactionDate = DatePickerTransactionDate.Text
    '                        .TransactionTypeID = transactionTypeID
    '                        .Comment = TextBoxComment.Text
    '                    End With
    '                    expensesDB.Transactions.InsertOnSubmit(saveNewTransaction)
    '                    expensesDB.SubmitChanges()

    '                    'if Petrol
    '                    If transactionTypeID = 1 Then
    '                        Dim saveNewPetrolTransaction As New Petrol
    '                        With saveNewPetrolTransaction
    '                            .Date = DatePickerTransactionDate.Text
    '                            .Amount = TextBoxAmount.Text
    '                            .TransactionID = saveNewTransaction.TransactionID
    '                            'defaults
    '                            .Vehicle = "1"
    '                            .Distance = 0
    '                            .Litres = 0
    '                            .AmountPerLitre = 0
    '                        End With
    '                        expensesDB.Petrols.InsertOnSubmit(saveNewPetrolTransaction)
    '                        expensesDB.SubmitChanges()

    '                        petrolTrans = New PetrolTransactionsClass(DatePickerTransactionDate.Text _
    '                                                              , Nothing _
    '                                                              , Nothing _
    '                                                              , Nothing _
    '                                                              , TextBoxAmount.Text _
    '                                                              , Nothing _
    '                                                              , FormatDateTime(DatePickerTransactionDate.Text.ToString, DateFormat.ShortDate) & " - " & FormatCurrency(TextBoxAmount.Text) _
    '                                                              , Nothing _
    '                                                              , Nothing _
    '                                                              , saveNewTransaction.TransactionID _
    '                                                              , TextBoxComment.Text)
    '                        Data.Add(petrolTrans)

    '                        Dim ied As IEnumerable(Of PetrolTransactionsClass) = From d In Data Order By d.TransactionDate Descending
    '                        For Each i In ied
    '                            Data.Remove(i)
    '                            Data.Add(i)
    '                        Next
    '                    End If


    '                    expenseTrans = New ExpensesClass(ComboBoxExpenseType.Text, TextBoxAmount.Text, DatePickerTransactionDate.Text, saveNewTransaction.TransactionID, transactionTypeID, CheckBoxOneOff.IsChecked, TextBoxComment.Text)
    '                    expenseList.Add(expenseTrans)
    '                    sortData()
    '                    editFlag = HelperClass.EditMode.Edit

    '                    Dim ctr As Integer = 0
    '                    For Each item In expenseList
    '                        If item.ExpenseId = saveNewTransaction.TransactionID Then
    '                            Exit For
    '                        End If
    '                        ctr += 1
    '                    Next

    '                    listBoxExpense.SelectedIndex = ctr

    '                ElseIf editFlag = HelperClass.EditMode.Edit Then
    '                    Dim selectedExpenseItem As Integer = listBoxExpense.SelectedValue.expenseid
    '                    'if Petrol
    '                    If transactionTypeID = 1 Then

    '                        Dim editTransaction = From petrol In expensesDB.Petrols _
    '                                              Where (petrol.MainId = selectedExpenseItem)

    '                        With editTransaction.First
    '                            .Petrol.Amount = TextBoxAmount.Text
    '                            .Petrol.Date = DatePickerTransactionDate.Text
    '                        End With
    '                        expensesDB.SubmitChanges()
    '                        With Data.Where(Function(x) x.TransactionID = selectedExpenseItem).First
    '                            .TransactionAmount = TextBoxAmount.Text
    '                            .TransactionDate = DatePickerTransactionDate.Text
    '                            .TransactionDetails = FormatDateTime(DatePickerTransactionDate.SelectedDate, DateFormat.ShortDate) & " - " & FormatCurrency(TextBoxAmount.Text)
    '                            .Comment = TextBoxComment.Text
    '                        End With
    '                    End If

    '                    Dim expenses = From trans In expensesDB.Transactions _
    '                          Join transType In expensesDB.TransactionTypes _
    '                          On trans.TransactionTypeID Equals transType.TransactionTypeID _
    '                          Where trans.TransactionID = selectedExpenseItem

    '                    With expenses.First
    '                        .trans.TransactionDate = DatePickerTransactionDate.Text
    '                        .trans.TransactionAmount = TextBoxAmount.Text
    '                        .trans.Comment = TextBoxComment.Text
    '                        .transType.OneOff = CheckBoxOneOff.IsChecked
    '                    End With
    '                    expensesDB.SubmitChanges()

    '                    'reflect changes in list
    '                    With expenseList.Where(Function(x) x.ExpenseId = selectedExpenseItem).First
    '                        .ExpenseAmount = TextBoxAmount.Text
    '                        .ExpenseDate = DatePickerTransactionDate.Text
    '                        .ExpenseOneOff = CheckBoxOneOff.IsChecked
    '                        .ExpenseComment = TextBoxComment.Text
    '                    End With
    '                End If
    '                frmIsDirty = False
    '                setButtonProperties()
    '                LoadSubHeader()


    '            Catch ex As Exception
    '                MessageBox.Show("Error encountered while saving the record." & ControlChars.Lf & ControlChars.Lf & ex.Message.ToString, "Save Error", MessageBoxButton.OK, MessageBoxImage.Error)

    '            End Try

    '        End If
    '    End Sub
    '#End Region

    '#Region "Support Procedures"
    '    Private Sub expenseTypeList_CollectionChanged(ByVal sender As Object, ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs) Handles expenseTypeList.CollectionChanged
    '        If listBoxExpense.SelectedIndex >= 0 Then
    '            With listBoxExpense.SelectedValue
    '                DatePickerTransactionDate.SelectedDate = .ExpenseDate
    '                ComboBoxExpenseType.SelectedValue = .ExpenseTypeId
    '                TextBoxAmount.Text = .ExpenseAmount
    '                CheckBoxOneOff.IsChecked = .expenseoneoff
    '                TextBoxComment.Text = .expenseComment
    '            End With

    '            mainOverlay.Visibility = Visibility.Collapsed
    '            editFlag = HelperClass.EditMode.Edit
    '            frmIsDirty = True
    '            setButtonProperties()
    '        Else
    '            HelperClass.ClearTextBox(GridExpense)
    '            HelperClass.ClearComboBox(GridExpense)
    '            DatePickerTransactionDate.SelectedDate = Nothing
    '        End If
    '    End Sub

    '    Public Function FilterCompletedTasks(ByVal t As Object) As Boolean
    '        Dim _tasks As New ExpensesClass
    '        _tasks = t
    '        Return _tasks.ExpenseType.ToUpper.Contains(searchBox.Text.ToUpper)
    '    End Function

    '    Sub sortData()
    '        Dim iexpense As IEnumerable(Of ExpensesClass) = From d In expenseList Order By d.ExpenseDate Descending
    '        For Each i In iexpense
    '            expenseList.Remove(i)
    '            expenseList.Add(i)
    '        Next
    '    End Sub

    '    Sub LoadExpenseTypes()
    '        'Dim expenseTypes = From xList In expensesDB.TransactionDescriptions _
    '        '                  Join tt In expensesDB.TransactionTypes _
    '        '                  On xList.TransactionDescriptionID Equals tt.TransactionDescID _
    '        '                  Where tt.TransactionTypeDescID = 2 _
    '        '                  Select expensesType = xList.TransactionDescription, ExpenseTypeId = xList.TransactionDescriptionID _
    '        '                  Distinct


    '        ComboBoxExpenseType.ItemsSource = expenseTypeList
    '        ComboBoxExpenseType.DisplayMemberPath = "ExpenseName"
    '        ComboBoxExpenseType.SelectedValuePath = "ExpenseTypeId"
    '    End Sub

    '    Private Sub HookupChangeEventHandlers()
    '        For Each expenseTrans As ExpensesClass In expenseList
    '            HookupChangeEventHandler(expenseTrans)
    '        Next
    '    End Sub

    '    Private Sub HookupChangeEventHandler(ByVal expenseTrans As ExpensesClass)
    '        AddHandler expenseTrans.PropertyChanged, _
    '          AddressOf HandlePropertyChanged
    '    End Sub

    '    Private Sub HandlePropertyChanged( _
    '    ByVal sender As Object, _
    '    ByVal e As PropertyChangedEventArgs)

    '        Dim propName As String = e.PropertyName
    '        Dim myexpense As ExpensesClass = CType(sender, ExpensesClass)

    '        Dim propInfo As System.Reflection.PropertyInfo = _
    '          GetType(ExpensesClass).GetProperty(propName)
    '        Dim value As Object = _
    '          propInfo.GetValue(myexpense, Nothing)

    '    End Sub

    '#End Region

    '#Region "Control Events"
    '    Private Sub searchBox_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles searchBox.TextChanged
    '        If Not listBoxExpense Is Nothing Then
    '            Dim icview As ICollectionView = CollectionViewSource.GetDefaultView(expenseList)
    '            'If icview.Filter Is Nothing Then
    '            icview.Filter = New Predicate(Of Object)(AddressOf FilterCompletedTasks)
    '            'Else
    '            'icview.Filter = Nothing
    '            'End If
    '        End If
    '    End Sub

    '    Private Sub searchBox_GotFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles searchBox.GotFocus
    '        If searchBox.Text = "Search" Then
    '            searchBox.Text = Nothing
    '        End If
    '    End Sub

    '    Private Sub listBoxExpense_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles listBoxExpense.SelectionChanged
    '        If listBoxExpense.SelectedIndex >= 0 Then
    '            With listBoxExpense.SelectedValue
    '                DatePickerTransactionDate.SelectedDate = .ExpenseDate
    '                ComboBoxExpenseType.SelectedValue = .ExpenseTypeId
    '                TextBoxAmount.Text = .ExpenseAmount
    '                CheckBoxOneOff.IsChecked = .expenseoneoff
    '                TextBoxComment.Text = .expenseComment
    '            End With

    '            mainOverlay.Visibility = Visibility.Collapsed
    '            editFlag = HelperClass.EditMode.Edit
    '            frmIsDirty = True
    '            setButtonProperties()
    '        Else
    '            HelperClass.ClearTextBox(GridExpense)
    '            HelperClass.ClearComboBox(GridExpense)
    '            DatePickerTransactionDate.SelectedDate = Nothing
    '        End If

    '    End Sub


    '    Private Sub CheckBoxOneOff_Unchecked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CheckBoxOneOff.Unchecked, CheckBoxOneOff.Checked
    '        frmIsDirty = True
    '        setButtonProperties()
    '    End Sub

    '    Private Sub ComboBox_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)
    '        Me.Title = ComboBoxExpenseType.Text & IIf(Not frmIsDirty, " (unsaved)", "")
    '        For itemIndex = 0 To oWindowList.Count - 1
    '            If oWindowList(itemIndex).WindowID = Me.Name Then
    '                oWindowList(itemIndex).WindowName = Me.Title
    '            End If
    '        Next
    '        pageIsValid(ComboBoxExpenseType.Name, EntryValidation.ValidateEditableCombo(ComboBoxExpenseType, comboClassTextbox))
    '        frmIsDirty = True
    '        setButtonProperties()
    '    End Sub

    '    Private Sub TextBoxAmount_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles TextBoxAmount.TextChanged
    '        pageIsValid(TextBoxAmount.Name, EntryValidation.ValidateIsNumeric(TextBoxAmount, True))
    '        frmIsDirty = True
    '        setButtonProperties()
    '    End Sub

    '    Private Sub DatePickerTransactionDate_SelectedDateChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles DatePickerTransactionDate.SelectedDateChanged
    '        pageIsValid(DatePickerTransactionDate.Name, EntryValidation.ValidateDatePicker(DatePickerTransactionDate))
    '        frmIsDirty = True
    '        setButtonProperties()
    '    End Sub

    '    Private Sub TextBoxComment_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles TextBoxComment.TextChanged
    '        frmIsDirty = True
    '        setButtonProperties()
    '    End Sub
    '#End Region

    '#Region "Set Defaults"
    '    Sub LoadSubHeader()

    '        If expenseList.Count > 1 Then
    '            ExpanderText.Text = expenseList.Count & " records found"
    '        Else
    '            ExpanderText.Text = expenseList.Count & " record found"
    '        End If

    '    End Sub

    '    Sub noSelectedData()
    '        listBoxExpense.SelectedIndex = -1
    '        frmIsDirty = False
    '        setButtonProperties()
    '        mainOverlay.Visibility = Visibility.Visible
    '    End Sub


    '    Sub setButtonProperties()
    '        If frmIsDirty Then
    '            RibbonButtonSave.IsEnabled = True
    '            RibbonButtonCancel.IsEnabled = True
    '            RibbonButtonDelete.IsEnabled = True
    '            'RibbonButtonNew.IsEnabled = False
    '        Else
    '            RibbonButtonSave.IsEnabled = False
    '            RibbonButtonCancel.IsEnabled = False
    '            RibbonButtonDelete.IsEnabled = False
    '            'RibbonButtonNew.IsEnabled = True
    '        End If
    '    End Sub

    '    Sub pageIsValid(ByVal sender As String, ByVal isValid As Boolean)
    '        Dim inArray As Boolean = False
    '        For i = 0 To al.Count - 1
    '            If al.Item(i) = sender Then
    '                If isValid Then
    '                    al.Remove(Trim(sender))
    '                End If
    '                inArray = True
    '                Exit For
    '            End If
    '        Next
    '        If Not inArray Then
    '            If Not isValid Then
    '                al.Add(Trim(sender))
    '            End If
    '        End If
    '        If al.Count > 0 Then
    '            _pageIsValid = False
    '        Else
    '            _pageIsValid = True
    '        End If
    '    End Sub
    '#End Region



    Private Sub RibbonButtonPetrol_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles RibbonButtonPetrol.Click
        Dim petrolWindow As New NewPetrol
        petrolWindow.Name = "window" & oWindowList.Count
        petrolWindow.Show()
        oWindow = New OpenedWindowClass(petrolWindow.Name, "Petrol", petrolWindow.Name, "Images/Gas-Station-icon.png")
        oWindowList.Add(oWindow)
    End Sub
End Class
